class ApplicationController < ActionController::Base
  protect_from_forgery

  def logged_in?
    current_user
  end

  def login_required
    unless logged_in?
      store_target_location
      redirect_to "/users/sign_in", :alert => "You must first log in or sign up before accessing this page."
    end
  end

  def redirect_to_target_or_default(default, *args)
    redirect_to(session[:return_to] || default, *args)
    session[:return_to] = nil
  end

  def set_current_user
    User.current = current_user
  end

  private

  def store_target_location
    session[:return_to] = request.url
  end
end
